library(tidyverse) # all of our usual tools
library(ggmap) # for spatial viz.
library(viridis) # for color scales
The number of earthquakes in Oklahoma has increased in the past decade
We will examine data obtained from the U.S. Geological Survey (USGS) on earthquakes in Oklahoma since 2000
ok_quakes <- readr::read_csv("OKquakes.csv")
str(ok_quakes)
## Classes 'tbl_df', 'tbl' and 'data.frame': 5204 obs. of 9 variables:
## $ time : POSIXct, format: "2015-12-31 20:31:14" "2015-12-31 11:35:26" ...
## $ latitude : num 35.8 36.6 35.7 36.8 35.7 ...
## $ longitude: num -97.6 -98.8 -97.4 -97.8 -97.4 ...
## $ depth : num 6.48 6.29 5.97 5.41 6.18 ...
## $ mag : num 2.6 3.2 2.5 3 2.8 2.5 3 3.2 3.1 3.3 ...
## $ place : chr "16km NNW of Edmond, Oklahoma" "24km SSW of Alva, Oklahoma" "6km ENE of Edmond, Oklahoma" "4km NW of Medford, Oklahoma" ...
## $ year : int 2015 2015 2015 2015 2015 2015 2015 2015 2015 2015 ...
## $ month : int 12 12 12 12 12 12 12 12 12 12 ...
## $ type : chr "very minor" "minor" "very minor" "minor" ...
ggplot2ggplot(data = filter(ok_quakes, year == 2015), aes(x = longitude, y = latitude)) +
geom_point()
ggmap packageContext is important, we need more than just the points!
ggmap allows us to grab this context from google or other sources
By default, get_map, downloads a terrain image from Google maps
# Download the requested map
ok_map <- get_map(location = 'oklahoma', zoom = 7)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=oklahoma&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=oklahoma&sensor=false
# Display the map
ggmap(ok_map)
get_map and read about the location and zoom arguments. Do the following:location = 'lawrence university' to get a map of the surrounding area. Try difference values for zoom.location argument and produce a map of the surrounding area. Again, try different values for zoom.There are other options aside from a terrain map.
get_map function and read about the maptype and source arguments. Specify different maptypes and sources. How do things change?Now that we have our map of Oklahoma, we can add points to it:
ggmap(ok_map) +
geom_point(data = ok_quakes, aes(x = longitude, y = latitude, color = year))
A better idea might be to facet the plot by year. In order to to this we need to add the base_layer arugment to the ggmap function:
ggmap(ok_map, base_layer = ggplot(ok_quakes, aes(x = longitude, y = latitude))) +
geom_point(size = .8) +
facet_wrap(~year, ncol = 5)
Add color to the facetted map based on the type of earthquake. Be sure to use an appropriate color palette. Do any patterns emerge?
The file CAquakes.csv contains data on earthquakes in California from January 1, 2010 through the end of 2015. Use ggmap to plot the earthquakes that occurred in California during this time, similar to what we did for Oklahoma above. Do any patterns emerge?
ok2015 <- filter(ok_quakes, year == 2015)
We can create heatmaps using stat_density2d in place of our geom_point layer:
ggmap(ok_map, base_layer = ggplot(ok2015, aes(x = longitude, y = latitude))) +
stat_density2d(aes(fill = ..level.., alpha = ..level..), size = 0.01,
bins = 16, geom = "polygon") +
scale_fill_viridis() +
scale_alpha(guide = FALSE)